package com.central.apps.handler.asset;

import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理资产领用业务逻辑
 *
 * @author lidachao
 * @date 2022/7/28
 */
@Component
@Slf4j
public class AssetCollectionHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if("approval".equals(flowNode.getId())) {
            // 查询资产领用主表信息
            String collSql = "select collnum, type from gq_assetcollhead where id = " + id;
            List<Map<String, Object>> collList = handlerService.findBySql(collSql);

            if (CollectionUtils.isNotEmpty(collList)) {
                Map collMap = collList.get(0);
                String collnum = (String) collMap.get("collnum");
                String type = (String) collMap.get("type");
                String status = "闲置";
                if ("领用".equals(type)) {
                    status = "使用中";
                }

                // 查询资产领用子表信息
                String collDtlSql = "select assetnum, assetuser, assetadmin, usesite from gq_assetcolldetail where collnum = '" + collnum +"';";
                List<Map<String, Object>> collDtlList = handlerService.findBySql(collDtlSql);

                if (CollectionUtils.isNotEmpty(collDtlList)) {
                    Map<String, Object> collDtlMap = new HashMap();
                    for (int i = 0; i < collDtlList.size(); i++) {
                        collDtlMap = collDtlList.get(i);
                        String assetnum = (String) collDtlMap.get("assetnum");
                        String assetuser = (String) collDtlMap.get("assetuser");
                        String assetadmin = (String) collDtlMap.get("assetadmin");
                        String usesite = (String) collDtlMap.get("usesite");

                        // 更新资产状态
                        String updAssetSql = "update gq_assethead set status = '" + status + "', assetuser = '" + assetuser
                                + "', assetadmin = '" + assetadmin + "', installsite = '" + usesite + "' where assetnum = '" + assetnum + "';";
                        handlerService.updateBySql(updAssetSql);
                    }
                }
            }
        }
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String collnum = MapUtils.getString(data,"collnum");
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_assetcolldetail where collnum ='" + collnum + "'");
        //0 :可用 1:禁用
        if(ToolUtil.isEmpty(bySql)){
            handlerService.updateBySql("update gq_assetcollhead set typestatus ='0' where collnum ='"+collnum+"'");
        }else{
            handlerService.updateBySql("update gq_assetcollhead set typestatus ='1' where collnum ='"+collnum+"'");
        }
    }
}
